<?php
/**
 * UserCategory Model
 *
 * @property User $User
 */
class Group extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'name';

	# Validation rules.
    var $validate = array(
        'name' => array(
            'notNull' =>array(
    		   'rule' => 'notEmpty',
    		   'required' => true,
    		   'message' => 'The Group Name must not be null and it is required',
    		   'last' => true
    		)
		)
	);
    

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'group_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
    
    var $hasAndBelongsToMany = array(
		'Module' => array(
			'className' => 'Module',
			'joinTable' => 'modules_groups',
			'foreignKey' => 'group_id',
			'associationForeignKey' => 'module_id',
			'unique' => true,
			'conditions' => array('Module.active'=>1, 'Module.installed'=>1),
			'fields' => '',
			'order' => array('Module.name'=>'ASC'),
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
    
    
    
    /**
     * This function retrieves all the users from the database tables users.
     * @name getAllUsers
     * @param void
     * @return mixed. Array containing the data retrieved.
     * @access public
     */
    function getAllGroups(){
    	# fetch the data and return it.
    	return $this->find('all', array('conditions' => array('Group.deleted' => 'n'), 'recursive' => -1));
    }

    /**
     * This function retrieves the user data based upon his/her id
     * @name getUserById
     * @param string $userId string holding the user id.his parameter is defaulted to null.
     * When it is null it means that cake is going to use the ActiveRecord system to retrieve the data.
     * @return mixed. Array containing the user data.
     * @access public
     */
    function getGroupById($id = null){
        $conditions = array(
           'Group.id' => $id,
           'Group.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return  $this->find('first', array('contain'=>array('Module'),'conditions' => $conditions, 'recursive' => 1));
    }
    
    
    function getGroupNameById($id = null){
        $conditions = array(
           'Group.id' => $id
        );
        # fetch the specific data from the server and retrun it.
        $result = $this->find('first', array('fields'=>array('name'),'conditions' => $conditions, 'recursive' => -1));
        return $result['Group']['name'];
    }
    
}
